package com.itheima.reggie.config;

import com.itheima.reggie.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * 拦截器配置
 *
 * @author Upjqs
 * @since 2024-02-01 21:03
 */
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Resource
    private LoginInterceptor loginInterceptor;

    /**
     * 设置静态资源映射
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始进行静态资源映射...");
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }

    /**
     * 添加拦截器配置
     *
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // 需要添加启动的拦截器
        registry.addInterceptor(loginInterceptor)
                // 拦截路径
                .addPathPatterns("/**")


                // 在拦截所有请求基础上，放行以下资源
                .excludePathPatterns("/backend/**")
                .excludePathPatterns("/front/**")
                .excludePathPatterns("/employee/login")
                .excludePathPatterns("/employee/logout")

                // 加上 用户登录行为的放行：
                .excludePathPatterns("/user/sendMsg")
                .excludePathPatterns("/user/login")

                // 文件操作相关 方便测试
                .excludePathPatterns("/common/**")


                .excludePathPatterns("/error");

    }


}
